
package framework;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class xmloutput {

  public static void generateOutput() throws Exception {
    generateXml();
    generateBarChart();
    generateHtml();
  }

  public static void generateXml() throws Exception {
    try {

      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      DOMImplementation impl = builder.getDOMImplementation();
      Document doc = impl.createDocument(null, null, null);

      Element testResultEelement = doc.createElement("TestResult");
      doc.appendChild(testResultEelement);
      Map<String, Set<String>> resultDetails = Resultdata.getModuleTestCaseMap();
      Iterator<String> itrModuleName = resultDetails.keySet().iterator();
      while (itrModuleName.hasNext()) {
        String moduleName = itrModuleName.next();

        Element moduleElement = doc.createElement("ModuleName");
        moduleElement.setAttribute("Name", moduleName);
        testResultEelement.appendChild(moduleElement);

        Set<String> liTestCases = resultDetails.get(moduleName);
        Iterator<String> itrTestCases = liTestCases.iterator();
        while (itrTestCases.hasNext()) {
          String testCaseName = itrTestCases.next();
          Element testCaseElement = doc.createElement("TestCase");
          testCaseElement.setAttribute("Name", testCaseName);
          moduleElement.appendChild(testCaseElement);
          Set<String> assertSet = Resultdata.getTestCaseAssertionMap().get(
              moduleName + testCaseName);
          if (assertSet != null) {
            Iterator<String> itr = assertSet.iterator();
            while (itr.hasNext()) {
              String assertionName = itr.next();

              String assertionStatus = Resultdata.getAssertionValueMap().get(
                  moduleName + testCaseName + assertionName);

              Element assertionElement = doc.createElement("Assertion");
              assertionElement.setAttribute("Name", assertionName);

              Element assertionStatusElement = doc.createElement("TestStatus");
              assertionStatusElement.setAttribute("Status", assertionStatus);

              assertionElement.appendChild(assertionStatusElement);
              testCaseElement.appendChild(assertionElement);
            }
          }
        }
      }
      XMLSerializer serializer = new XMLSerializer();
//      serializer.setOutputCharStream(writer)
      	
      	File file = new File("src/output/test1.xml");
      	file.createNewFile();
        FileWriter fi=new FileWriter(file);
      serializer.setOutputCharStream(fi);
      serializer.serialize(doc);   

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public static StringBuilder  generateHtml() throws Exception {
    try {
      int totalNoOfTestCases = 0;
      int totalNoOfAssertion = 0;
      int totalNoOfPassed = 0;
      int totalNoOfFailed = 0;
      StringBuilder htmlBuilder = new StringBuilder("<Html>");
      htmlBuilder.append("<head>");
      htmlBuilder.append("<title>");
      htmlBuilder.append("Test Report Conducted on " + new Date().toString());
      htmlBuilder.append("</title>");
      htmlBuilder.append("</head>");
      htmlBuilder.append("<body>");
      htmlBuilder.append("<table align=\"left\">");
      htmlBuilder.append("<tr><!-- first row  -->");
      htmlBuilder.append("<td>");
      htmlBuilder.append("<table>");
      htmlBuilder.append("<tr><td><b><Center>Test Execution Summary</Center></b></td></tr>");

      htmlBuilder.append("<tr><table border=\"1\">");
      htmlBuilder.append("<tr bgcolor=\"#9acd32\">");
      htmlBuilder.append("<th>Module Name</th>");
      htmlBuilder.append("<th>Test Cases</th>");
      htmlBuilder.append("<th>Assertion</th>");
      htmlBuilder.append("<th>Passed</th>");
      htmlBuilder.append("<th>Failed</th>");
      htmlBuilder.append("</tr>");

      Map<String, Set<String>> resultDetails = Resultdata.getModuleTestCaseMap();
      Iterator<String> itrModuleName = resultDetails.keySet().iterator();
      while (itrModuleName.hasNext()) {
        int noOfTestCases = 0;
        int noOfAssertion = 0;
        int noOfPassed = 0;
        int noOfFailed = 0;
        htmlBuilder.append("<tr>");
        String moduleName = itrModuleName.next();

        htmlBuilder.append("<td>" + moduleName + "</td>");

        Set<String> liTestCases = resultDetails.get(moduleName);
        noOfTestCases = liTestCases.size();
        htmlBuilder.append("<td>" + noOfTestCases + "</td>");

        Iterator<String> itrTestCases = liTestCases.iterator();
        while (itrTestCases.hasNext()) {
          String testCaseName = itrTestCases.next();
          Set<String> assertSet = Resultdata.getTestCaseAssertionMap().get(
              moduleName + testCaseName);
          if (assertSet != null) {
            noOfAssertion = noOfAssertion + assertSet.size();
            Iterator<String> itr = assertSet.iterator();
            while (itr.hasNext()) {
              String assertionName = itr.next();
              String assertionStatus = Resultdata.getAssertionValueMap().get(
                  moduleName + testCaseName + assertionName);
              if (assertionStatus.equals("Failed")) {
                ++noOfFailed;
              } else {
                ++noOfPassed;
              }
            }
          }
        }
        htmlBuilder.append("<td>" + noOfAssertion + "</td>");
        htmlBuilder.append("<td>" + noOfPassed + "</td>");
        htmlBuilder.append("<td>" + noOfFailed + "</td>");
        totalNoOfTestCases = totalNoOfTestCases + noOfTestCases;
        totalNoOfAssertion = totalNoOfAssertion + noOfAssertion;
        totalNoOfPassed = totalNoOfPassed + noOfPassed;
        totalNoOfFailed = totalNoOfFailed + noOfFailed;
        htmlBuilder.append("</tr>");
      }
      htmlBuilder.append("<tr>");
      htmlBuilder.append("<td>Total Test Cases </td>");
      htmlBuilder.append("<td>" + totalNoOfTestCases + "</td>");
      htmlBuilder.append("<td>" + totalNoOfAssertion + "</td>");
      htmlBuilder.append("<td>" + totalNoOfPassed + "</td>");
      htmlBuilder.append("<td>" + totalNoOfFailed + "</td>");
      htmlBuilder.append("</tr>");
      htmlBuilder.append("</table>");
      htmlBuilder.append("</tr>");
      htmlBuilder.append("</table>");
      htmlBuilder.append("</td>");
      htmlBuilder.append("<td><img src=\"barChart.jpg\" alt=\"Bar Chart\" /></td>");
      htmlBuilder.append("</tr> <!-- first row close -->");
      htmlBuilder.append("</table>");
      htmlBuilder.append("</body>");
      htmlBuilder.append("</Html>");
//
//      String s = htmlBuilder.toString();
//
//      File file = new File("src/output/output.html");
//      file.createNewFile();
//
//      PrintStream printStream = new PrintStream(file);
//      printStream.print(s);
//      printStream.flush();
//      printStream.close();
//
      return htmlBuilder;
      
    } catch (Exception ex) {
      ex.printStackTrace();
    }
      
      return null;
  }

  
  public static StringBuilder  generateHtml1() throws Exception {
	    try {
	      int totalNoOfTestCases = 0;
	      int totalNoOfAssertion = 0;
	      int totalNoOfPassed = 0;
	      int totalNoOfFailed = 0;
	      StringBuilder htmlBuilder = new StringBuilder("<Html>");
	      htmlBuilder.append("<head>");
	      htmlBuilder.append("<title>");
	      htmlBuilder.append("Test Report Conducted on " + new Date().toString());
	      htmlBuilder.append("</title>");
	      htmlBuilder.append("</head>");
	      htmlBuilder.append("<body>");
	      htmlBuilder.append("<table align=\"left\">");
	      htmlBuilder.append("<tr><!-- first row  -->");
	      htmlBuilder.append("<td>");
	      htmlBuilder.append("<table>");
	      htmlBuilder.append("<tr><td><b><Center>Test Execution Summary</Center></b></td></tr>");

	      htmlBuilder.append("<tr><table border=\"1\">");
	      htmlBuilder.append("<tr bgcolor=\"#9acd32\">");
	      htmlBuilder.append("<th>Module Name</th>");
	      htmlBuilder.append("<th>Test Cases</th>");
	      htmlBuilder.append("<th>Assertion</th>");
	      htmlBuilder.append("<th>Passed</th>");
	      htmlBuilder.append("<th>Failed</th>");
	      htmlBuilder.append("</tr>");

	      Map<String, Set<String>> resultDetails = Resultdata.getModuleTestCaseMap();
	      Iterator<String> itrModuleName = resultDetails.keySet().iterator();
	      while (itrModuleName.hasNext()) {
	        int noOfTestCases = 0;
	        int noOfAssertion = 0;
	        int noOfPassed = 0;
	        int noOfFailed = 0;
	        htmlBuilder.append("<tr>");
	        String moduleName = itrModuleName.next();

	        htmlBuilder.append("<td>" + moduleName + "</td>");

	        Set<String> liTestCases = resultDetails.get(moduleName);
	        noOfTestCases = liTestCases.size();
	        htmlBuilder.append("<td>" + noOfTestCases + "</td>");

	        Iterator<String> itrTestCases = liTestCases.iterator();
	        while (itrTestCases.hasNext()) {
	          String testCaseName = itrTestCases.next();
	          Set<String> assertSet = Resultdata.getTestCaseAssertionMap().get(
	              moduleName + testCaseName);
	          if (assertSet != null) {
	            noOfAssertion = noOfAssertion + assertSet.size();
	            Iterator<String> itr = assertSet.iterator();
	            while (itr.hasNext()) {
	              String assertionName = itr.next();
	              String assertionStatus = Resultdata.getAssertionValueMap().get(
	                  moduleName + testCaseName + assertionName);
	              if (assertionStatus.equals("Failed")) {
	                ++noOfFailed;
	              } else {
	                ++noOfPassed;
	              }
	            }
	          }
	        }
	        htmlBuilder.append("<td>" + noOfAssertion + "</td>");
	        htmlBuilder.append("<td>" + noOfPassed + "</td>");
	        htmlBuilder.append("<td>" + noOfFailed + "</td>");
	        totalNoOfTestCases = totalNoOfTestCases + noOfTestCases;
	        totalNoOfAssertion = totalNoOfAssertion + noOfAssertion;
	        totalNoOfPassed = totalNoOfPassed + noOfPassed;
	        totalNoOfFailed = totalNoOfFailed + noOfFailed;
	        htmlBuilder.append("</tr>");
	      }
	      htmlBuilder.append("<tr>");
	      htmlBuilder.append("<td>Total Test Cases </td>");
	      htmlBuilder.append("<td>" + totalNoOfTestCases + "</td>");
	      htmlBuilder.append("<td>" + totalNoOfAssertion + "</td>");
	      htmlBuilder.append("<td>" + totalNoOfPassed + "</td>");
	      htmlBuilder.append("<td>" + totalNoOfFailed + "</td>");
	      htmlBuilder.append("</tr>");
	      htmlBuilder.append("</table>");
	      htmlBuilder.append("</tr>");
	      htmlBuilder.append("</table>");
	      htmlBuilder.append("</td>");
	      htmlBuilder.append("<td><img src=\"cid:image\"></td>");
	      htmlBuilder.append("</tr> <!-- first row close -->");
	      htmlBuilder.append("</table>");
	      htmlBuilder.append("</body>");
	      htmlBuilder.append("</Html>");
	//
//	      String s = htmlBuilder.toString();
	//
//	      File file = new File("src/output/output.html");
//	      file.createNewFile();
	//
//	      PrintStream printStream = new PrintStream(file);
//	      printStream.print(s);
//	      printStream.flush();
//	      printStream.close();
	//
	      return htmlBuilder;
	      
	    } catch (Exception ex) {
	      ex.printStackTrace();
	    }
	      
	      return null;
	  }
  public static void generateBarChart() throws Exception {
    try {
      int totalNoOfTestCases = 0;
      int totalNoOfPassed = 0;
      int totalNoOfFailed = 0;

      String seriesTestCase = "Test Cases";
      String seriesPassed = "Passed";
      String seriesFailed = "Failed";
      DefaultCategoryDataset dataset = new DefaultCategoryDataset();

      Map<String, Set<String>> resultDetails = Resultdata.getModuleTestCaseMap();
      Iterator<String> itrModuleName = resultDetails.keySet().iterator();
      while (itrModuleName.hasNext()) {
        int noOfTestCases = 0;
        int noOfPassed = 0;
        int noOfFailed = 0;
        String moduleName = itrModuleName.next();

        Set<String> liTestCases = resultDetails.get(moduleName);
        noOfTestCases = liTestCases.size();

        Iterator<String> itrTestCases = liTestCases.iterator();
        while (itrTestCases.hasNext()) {
          String testCaseName = itrTestCases.next();

          Set<String> assertSet = Resultdata.getTestCaseAssertionMap().get(
              moduleName + testCaseName);

          if (assertSet != null) {
            Iterator<String> itr = assertSet.iterator();
            while (itr.hasNext()) {
              String assertionName = itr.next();
              String assertionStatus = Resultdata.getAssertionValueMap().get(
                  moduleName + testCaseName + assertionName);
              if (assertionStatus.equals("Failed")) {
                ++noOfFailed;
              } else {
                ++noOfPassed;
              }
            }
          }
        }
        dataset.addValue(noOfTestCases, seriesTestCase, moduleName);
        dataset.addValue(noOfPassed, seriesPassed, moduleName);
        dataset.addValue(noOfFailed, seriesFailed, moduleName);

        totalNoOfTestCases = totalNoOfTestCases + noOfTestCases;
        totalNoOfPassed = totalNoOfPassed + noOfPassed;
        totalNoOfFailed = totalNoOfFailed + noOfFailed;
      }

      JFreeChart chart = ChartFactory.createBarChart3D("TestReport", "Domain", "Value ($)",
          dataset, PlotOrientation.VERTICAL, true, true, false);

      try {
        File file = new File("src/output/barChart.jpg");
        if (!file.exists()) {
          file.createNewFile();
        }
        ChartUtilities.saveChartAsJPEG(file, chart, 500, 300);
      } catch (IOException e) {
        System.err.println("Problem occurred creating chart.");
      }

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

}